<template>
  <div id="main">
    <h2>教学计划管理</h2>
    <div class="head">
      <el-button type="primary" style="margin: 20px" @click="handleOpen('添加教学计划', 1)">添加教学计划</el-button>
      <el-input v-model="searchTableInfo" placeholder="请输入搜索内容" style="width:240px;float: right;margin:20px;" />
    </div>
    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column label="序号" type="index" align="center">
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          label="计划id"
          align="center"
          prop="planId"
        />
        <el-table-column
          align="center"
          prop="planTime"
          label="计划制定日期"
        />
        <el-table-column
          align="center"
          prop="staffName"
          label="计划实施人员"
        />

        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="handleOpen('修改计划', 2, Scope.row.planId)"
            >查看/编辑</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(Scope.row.planId)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5, 10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <!-- 基本信息、评估、训练 -->
    <el-dialog title="" :close-on-click-modal="false" :visible.sync="dialogFormVisible" :before-close="handleClose">
      <el-form ref="plan" :model="plan" label-position="top" :rules="rules">
        <!-- <el-form-item prop="planId" label="计划id" :label-width="formLabelWidth">
          <el-input v-model="plan.planId" autocomplete="off" />
        </el-form-item> -->
        <el-form-item prop="planStudentId" label="学生" :label-width="formLabelWidth">
          <el-select v-model="plan.plan.planStudentId" placeholder="请选择">
            <el-option
              v-for="item in child"
              :key="item.studentId"
              :label="item.String"
              :value="item.studentId"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="planTime" label="计划制定日期">
          <div class="block">
            <div class="demonstration">{{ planTime }}</div>
            <el-date-picker
              v-model="plan.plan.planTimeToString"
              type="date"
              placeholder="选择时间"
              format="yyyy -MM - dd"
              value-format="yyyy-MM-dd"
              style="width:245px"
            />
          </div>
        </el-form-item>

        <el-form-item prop="planExcutecycle" label="计划执行周期" :label-width="formLabelWidth">
          <el-input v-model="plan.plan.planExcutecycle" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="planStage" label="计划执行阶段" :label-width="formLabelWidth">
          <el-input v-model="plan.plan.planStage" autocomplete="off" />
        </el-form-item>

        <el-form-item prop="planPeopleId" label="计划实施人员" :label-width="formLabelWidth">
          <el-select v-model="plan.plan.planPeopleId" placeholder="请选择">
            <el-option
              v-for="item in staff"
              :key="item.staffId"
              :label="item.String"
              :value="item.staffId"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="planTeacherleaderId" label="计划制定人员(教学主管)" :label-width="formLabelWidth">
          <el-select v-model="plan.plan.planTeacherleaderId" placeholder="请选择教学主管">
            <el-option
              v-for="item in staff"
              :key="item.staffId"
              :label="item.String"
              :value="item.staffId"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="planRecoveryleaderId" label="计划制定人员(康复主管)" :label-width="formLabelWidth">
          <el-select v-model="plan.plan.planRecoveryleaderId" placeholder="请选择康复主管">
            <el-option
              v-for="item in staff"
              :key="item.staffId"
              :label="item.String"
              :value="item.staffId"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="planRecoveryleaderId" label="计划制定人员(医师主管)" :label-width="formLabelWidth">
          <el-select v-model="plan.plan.planPhysicianleaderId" placeholder="请选择医师主管">
            <el-option
              v-for="item in staff"
              :key="item.staffId"
              :label="item.String"
              :value="item.staffId"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="planExplaintime" label="讲解日期">
          <div class="block">
            <div class="demonstration">{{ planExplaintime }}</div>
            <el-date-picker
              v-model="plan.plan.planExplaintimeToString"
              type="date"
              placeholder="选择时间"
              format="yyyy -MM - dd"
              value-format="yyyy-MM-dd"
              style="width:245px"
            />
          </div>
        </el-form-item>
        <el-form-item prop="planExplainpeopleId" label="讲解人员" :label-width="formLabelWidth">
          <el-select v-model="plan.plan.planExplainpeopleId" placeholder="请选择">
            <el-option
              v-for="item in staff"
              :key="item.staffId"
              :label="item.String"
              :value="item.staffId"
            />
          </el-select>
          <el-form-item id="xunlian"><label style="font-size=20px">------------ ------------ ------------ 计划评估------------ ------------ ------------</label></el-form-item>

        <!-- </el-form-item>
        <el-form-item prop="planSummarizeId" label="教学计划id" :label-width="formLabelWidth">
          <el-input v-model="plan.plan.planSummarizeId" maxlength="30" autocomplete="off" placeholder="不超过30个字" />
        </el-form-item>
 -->

        </el-form-item>
        <el-form-item prop="planAccessmenttype" label="评估类型" :label-width="formLabelWidth">
          <el-input v-model="plan.plan.planAccessmenttype" maxlength="30" autocomplete="off" placeholder="不超过30个字" />
        </el-form-item>
        <el-form-item prop="planAccessmentinfo" label="评估综述" :label-width="formLabelWidth">
          <el-input v-model="plan.plan.planAccessmentinfo" maxlength="200" autocomplete="off" placeholder="不超过200个字" />
        </el-form-item>
        <el-form-item prop="areaId" label="评估计划领域" :label-width="formLabelWidth">
          <el-select v-model="plan.plan.area.areaId" placeholder="请选择">
            <el-option
              v-for="item in areaIds"
              :key="item.areaId"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="areaAdvantages" label="优势" :label-width="formLabelWidth">
          <el-input v-model="plan.plan.area.areaAdvantages" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="areaDisadvantages" label="劣势" :label-width="formLabelWidth">
          <el-input v-model="plan.plan.area.areaDisadvantages" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="areaRemarks" label="备注" :label-width="formLabelWidth">
          <el-input v-model="plan.plan.area.areaRemarks" autocomplete="off" />
        </el-form-item>

        <!-- 训练计划综述下表格 -->
        <el-form-item id="xunlian"><label style="font-size=20px">------------ ------------ ------------ 训练计划------------ ------------ ------------</label></el-form-item>

        <el-form-item prop="planSummarize" label="训练计划综述" :label-width="formLabelWidth" style="margin-Top: 20px;">
          <el-input v-model="plan.plan.planSummarize" maxlength="200" autocomplete="off" placeholder="不超过200个字" />
        </el-form-item>

        <el-form-item prop="areaTypeId" label="训练计划领域类型" :label-width="formLabelWidth">
          <el-select v-model="plan.plan.areaType.areaTypeId" placeholder="请选择">
            <el-option
              v-for="item in areaType"
              :key="item.areaTypeId"
              :label="item.areaName"
              :value="item.areaTypeId"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="planSummarizeTableTeachprogram" label="教学项目" :label-width="formLabelWidth">
          <el-input v-model="plan.plan.planSummarizeTable.planSummarizeTableTeachprogram" maxlength="200" autocomplete="off" placeholder="不超过200个字" />
        </el-form-item>
        <el-form-item prop="planSummarizeTableTeachactivity" label="教学活动" :label-width="formLabelWidth">
          <el-input v-model="plan.plan.planSummarizeTable.planSummarizeTableTeachactivity" maxlength="200" autocomplete="off" placeholder="不超过200个字" />
        </el-form-item>
        <el-form-item prop="planSummarizeTableFinishstandard" label="完成标准" :label-width="formLabelWidth">
          <el-input v-model="plan.plan.planSummarizeTable.planSummarizeTableFinishstandard" maxlength="200" autocomplete="off" placeholder="不超过200个字" />
        </el-form-item>
        <el-form-item prop="planSummarizeTableStarttime" label="开始日期">
          <div class="block">
            <div class="demonstration">{{ planSummarizeTableStarttime }}</div>
            <el-date-picker
              v-model="plan.plan.planSummarizeTable.planSummarizeTableStarttimeToString"
              type="date"
              placeholder="选择时间"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              style="width:245px"
            />
          </div>
        </el-form-item>
        <el-form-item prop="planSummarizeTableFinishtime" label="完成日期">
          <div class="block">
            <div class="demonstration">{{ planSummarizeTableFinishtime }}</div>
            <el-date-picker
              v-model="plan.plan.planSummarizeTable.planSummarizeTableFinishtimeToString"
              type="date"
              placeholder="选择时间"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              style="width:245px"
            />
          </div>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose">取 消</el-button>
        <el-button v-if="editType == 2" type="primary" @click="handleUpdate('plan')">修 改</el-button>
        <el-button v-if="editType == 1" type="primary" @click="handleAddUser('plan')">添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAllarea, addarea, updatearea, getareaById, deletearea } from '@/api/area'
import { getAllareaType, addareaType, updateareaType, getareaTypeById, deleteareaType } from '@/api/areaType'
import { getAllplanSummarizeTable, addplanSummarizeTable, updanpteplanSummarizeTable, getplanSummarizeTableById, deleteplanSummarizeTable } from '@/api/planSummarizeTable'
import { getAllplan, addplan, updateplan, getplanById, deleteplan } from '@/api/plan'
import { selectStudent } from '@/api/child'
import { selectstaff } from '@/api/staff'

export default {
  data() {
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      // list: [],
      areaType: [],
      tableList: [],
      listLoading: false,
      dialogFormVisible: false,
      editType: 1, // 1 添加 2 修改
      searchTableInfo: '',
      plan: {},
      plan: {
        plan: {
          area: {

          },
          areaType: {

          },
          planSummarizeTable: {

          }
        }
      },
      area: {},
      child: {},
      staff: {},
      areaType: {},
      newplan: {
        planId: '',
        // planTime: '',
        planPeopleName: ''
      },
      formLabelWidth: '200px',
      rules: {
        // planTime: [
        //   { required: true, message: '计划制定时间不能为空', trigger: 'blur' }
        // ],
        // planPeopleId: [
        //   { required: true, message: '计划实施人员不能为空', trigger: 'blur' }
        // ],
        // planExcutecycle: [
        //   { required: true, message: '计划周期不能为空', trigger: 'blur' }
        // ],
        // planStage: [
        //   { required: true, message: '突发事件地点不能为空', trigger: 'blur' }
        // ],
        // planTeacherleaderId: [
        //   { required: true, message: '教学主管能为空', trigger: 'blur' }
        // ],
        // planExplainpeopleId: [
        //   { required: true, message: '讲解人员不能为空', trigger: 'blur' }
        // ],
        // planPhysicianleaderId: [
        //   { required: true, message: '主管医师不能为空', trigger: 'blur' }
        // ],
        // planRecoveryleaderId: [
        //   { required: true, message: '康复主管不能为空', trigger: 'blur' }
        // ],
        // planAccessmenttype: [
        //   { required: true, message: '评估类型不能为空', trigger: 'blur' }
        // ],
        // planAccessmentinfo: [
        //   { required: true, message: '评估综述不能为空', trigger: 'blur' }
        // ],
        // planSummarizeTableFinishStandard:[
        //   { required: true, message: '完成标准不能为空', trigger: 'blur' }
        // ],
        // planSummarizeTableStarttime:[
        //   { required: true, message: '开始时间不能为空', trigger: 'blur' }
        // ],
        // planSummarizeTableFinishtime:[
        //   { required: true, message: '完成时间不能为空', trigger: 'blur' }
        // ],

      },

      // 领域选择
      areaIds: [
        {
          value: 0,
          label: '社会适应性'
        },
        {
          value: 1,
          label: '感知觉'
        },
        {
          value: 2,
          label: '认知领域'
        },
        {
          value: 3,
          label: '语言领域'
        },
        {
          value: 4,
          label: '精细'
        },
        {
          value: 5,
          label: '粗大'
        },
        {
          value: 6,
          label: '运动'
        },
        {
          value: 7,
          label: '生活自理'
        }
      ],
      planTime: '',
      planExplaintime: '',
      planSummarizeTableStarttime: '',
      planSummarizeTableFinishtime: ''
    }
  },
  computed: {
    // 根据计算属性模糊搜索
    list() {
      const searchTableInfo = this.searchTableInfo
      if (searchTableInfo) {
        // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
        // 注意： filter() 不会对空数组进行检测。
        // 注意： filter() 不会改变原始数组。
        return this.tableList.filter(data => {
          console.log('success' + data)
          // some() 方法用于检测数组中的元素是否满足指定条件;
          // some() 方法会依次执行数组的每个元素：
          // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
          // 如果没有满足条件的元素，则返回false。
          // 注意： some() 不会对空数组进行检测。
          // 注意： some() 不会改变原始数组。
          return Object.keys(data).some(key => {
            // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
            // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
            return String(data[key]).toLowerCase().indexOf(searchTableInfo) > -1
          })
        })
      }
      return this.tableList
    }
  },
  created() {
    this.getinfo()
    this.fetchData()
    this.getAllareaType()
  },

  methods: {
    getinfo: function() {
      selectStudent().then((resp) => {
        this.child = resp.obj
        console.log(this.child)
      }),
      selectstaff().then((resp) => {
        this.staff = resp.obj
        console.log(this.staff)
      })
    },
    getAllareaType() {
      getAllareaType().then((resp) => {
        this.areaType = resp.obj
        console.log('areaType:', this.areaType)
      })
    },
    fetchData() {
      this.tableList = []
      // this.listLoading = true;
      getAllplan().then(data => {
        console.log('data', data)
        for (var i = 0; i < data.obj.length; i++) {
          this.newplan.planPeopleId = data.obj[i].planPeopleId
          // this.newplan.planStudentId = data.obj[i].plan.planStudentId
          this.newplan.planTime = data.obj[i].plan.planTime
          // this.newplan.planPeopleName = data.obj[i].planPeopleName
          this.newplan.planId = data.obj[i].plan.planId
          // this.newplan.planExcutecycle=data.obj[i].plan.planExcutecycle
          // this.newplan.planAccessmentinfo = data.obj[i].plan.planAccessmentinfo
          // this.newplan.planAccessmenttype = data.obj[i].plan.planAccessmenttype
          // this.newplan.planAreaId = data.obj[i].plan.planAreaId
          // this.newplan.planExplainpeopleId = data.obj[i].plan.planExplainpeopleId
          // this.newplan.planExplaintime = data.obj[i].plan.planExplaintimeime
          // this.newplan.planPhysicianleaderId = data.obj[i].plan.planPhysicianleaderId
          // this.newplan.planRecoveryleaderId = data.obj[i].plan.planRecoveryleaderId
          // this.newplan.planSummarizeTableFinishStandard = data.obj[i].plan.planSummarizeTableFinishStandard
          // this.newplan.planSummarizeTableTeachActivity = data.obj[i].plan.planSummarizeTableTeachActivity
          // this.newplan.planSummarizeTableTeachprogram = data.obj[i].plan.planSummarizeTableTeachprogram
          // this.newplan.planSummarizeTableStarttime = data.obj[i].plan.planSummarizeTableStarttime
          // this.newplan.planSummarizeTableFinishtime = data.obj[i].plan.planSummarizeTableFinishtime
          // this.newplan.planStage = data.obj[i].plan.planStage
          // this.newplan.planSummarizeTableAreaTypeId = data.obj[i].plan.planSummarizeTableAreaTypeId
          // this.newplan.planSummarizeTableId = data.obj[i].plan.planSummarizeTableId
          // this.newplan.planTeacherleaderId = data.obj[i].planTeacherleaderId
          // this.newplan.areaTypeId = data.obj[i].plan.areaTypeId
          // // this.newplan.areaName = data.obj[i].plan.areaName
          // this.newplan.areaAdvantages = data.obj[i].plan.areaAdvantages
          // this.newplan.areaDisadvantages = data.obj[i].plan.areaDisadvantages
          // this.newplan.areaRemarks = data.obj[i].plan.areaRemarks
          // this.newplan.areaId = data.obj[i].plan.areaId
          // this.newplan.areaStudentId = data.obj[i].areaStudentId
          this.newplan.staffName = data.obj[i].plan.staff.staffName
          if (data.obj[i].areaIds === 0) {
            this.newexcute.areaIds = '社会适应性'
          }
          if (data.obj[i].areaIds === 1) {
            this.newexcute.areaIds = '感知觉'
          }
          if (data.obj[i].areaIds === 2) {
            this.newexcute.areaIds = '认知领域'
          }
          if (data.obj[i].areaIds === 3) {
            this.newexcute.areaIds = '语言领域'
          }
          if (data.obj[i].areaIds === 4) {
            this.newexcute.areaIds = '精细'
          }
          if (data.obj[i].areaIds === 5) {
            this.newexcute.areaIds = '粗大'
          }
          if (data.obj[i].areaIds === 6) {
            this.newexcute.areaIds = '运动'
          }
          if (data.obj[i].areaIds === 7) {
            this.newexcute.areaIds = '生活自理'
          }
          this.tableList.push(this.newplan)
          this.newplan = {}
        }
        console.log(this.tableList)
        this.listLoading = false
      })
    },
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    handleDelete(id) {
      this.$confirm('确认删除该计划？').then(() =>
        deleteplan(id).then((data) => {
          this.$notify({
            title: '操作成功',
            type: 'success'
          })
          const totalPage = Math.ceil((this.list.length - 1) / this.pagesize)
          const page =
            this.currentPage > totalPage ? totalPage : this.currentPage
          this.currentPage = this.currentPage < 1 ? 1 : page
          this.fetchData()
        }).catch(resp => {})
      )
    },
    handleAddUser(formName) {
      console.log('点击添加发送的信息', this.plan)

      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log('添加1', this.plan)
          // this.plan.plantimeToString = this.plan.plantime
          // this.plan.planExplaintimeToString = this.plan.planExplaintime
          // this.plan.planSummarizeTableStarttimeToString = this.plan.planSummarizeTableStarttime
          // this.plan.planSummarizeTableFinishtimeToString = this.plan.planSummarizeTableFinishtime
          addplan(this.plan).then((resp) => {
            this.$notify({
              title: '操作成功计划',
              type: 'success'
            })
            this.fetchData()
          }).catch(resp => {
          })
          addarea(this.plan).then((resp) => {
            this.$notify({
              title: '操作成功领域',
              type: 'success'
            })
            this.fetchData()
          }).catch(resp => {
          })
          addplanSummarizeTable(this.plan).then((resp) => {
            this.$notify({
              title: '操作成功训练计划',
              type: 'success'
            })
            this.fetchData()
          }).catch(resp => {
          })
          this.plan = {
            plan: {
              area: {

              },
              areaType: {

              },
              planSummarizeTable: {

              }
            }
          }
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleOpen(title, type, id) {
      this.plan = {
        plan: {
          area: {

          },
          areaType: {

          },
          planSummarizeTable: {

          }
        }
      }
      this.title = title
      this.editType = type

      if (type === 2) {
        console.log('发送的ID为', id)
        getplanById(id).then((resp) => {
          this.plan = resp.obj
          console.log('根具iD返回的信息', this.plan)
          // getplanbyId(id).then(resp => {
          // console.log(resp);
          // this.planTime = resp.obj;
          // this.plan.planExcutecycle = String(this.plan.planExcutecycle);
          // console.log(this.attendanceStu);
          // getStudentString(this.attendanceStu.classId).then(resp => {
          //   this.child = resp.obj;
        })
        this.dialogFormVisible = true
      } else {
        this.dialogFormVisible = true
      }
    },
    handleClose() {
      this.dialogFormVisible = false
      this.plan = {
        plan: {
          area: {

          },
          areaType: {

          },
          planSummarizeTable: {

          }
        }
      }
    },
    handleUpdate(formName, plan) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log('发送的plan', plan.planId)
          // this.plan.plantimeToString = this.plan.plantime
          // this.plan.planExplaintimeToString = this.plan.planExplaintime
          // this.plan.planSummarizeTableStarttimeToString = this.plan.planSummarizeTableStarttime
          // this.plan.planSummarizeTableFinishtimeToString = this.plan.planSummarizeTableFinishtime
          updateplan(plan.planId).then((resp) => {
            console.log('根据ID查看的信息', resp)
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            // this.fetchData()
          }).catch(resp => {
            this.fetchData()
          })
          updatearea(this.plan).then((resp) => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })
            this.fetchData()
          }).catch(resp => {
          })
          updateplanSummarizeTable(this.plan).then((resp) => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })
            this.fetchData()
          }).catch(resp => {
            //               this.$notify({
            //               title: "修改成功",
            //               type: "success"
            //               });
            //             this.fetchData();
          })
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    areaId(row, column) {
      console.log(this.row, hhh)
      if (row.areaIds === 0) {
        return '社会适应性'
      }
      if (row.areaIds === 1) {
        return '感知觉'
      }
      if (row.areaIds === 2) {
        return '认知领域'
      }
      if (row.areaIds === 3) {
        return '语言领域'
      }
      if (row.areaId === 4) {
        return '精细'
      }
      if (row.areaIds === 5) {
        return '粗大'
      }
      if (row.areaIds === 6) {
        return '运动'
      }
      if (row.areaIds === 7) {
        return '生活自理'
      }
    }
  }
}
</script>

<style scoped>
#main {
  margin-left: 5px;
  margin-right: 5px;
}
#table-area {
  margin-left: 10px;
  margin-right: 10px;
}
/* #shijian{
  display: flex;
  margin-top: 10px;
} */
#xunlian{
  font-size: 30px;
  margin-top: 30px;
  text-align: center;
}

</style>
